/*
 * @Author: Starfish_DL
 * @Date: 2020-10-26 17:28:47
 * @LastEditors: Starfish_DL
<<<<<<< HEAD
 * @LastEditTime: 2020-10-26 20:21:44
=======
 * @LastEditTime: 2020-10-26 20:18:39
>>>>>>> 09cbbba3d820f2fef0ed7fa9f28119c99f130391
 * @CurrentVersion: 1.0
 * @Description: 
 */

#include<iostream>
using namespace std;
#define MAXSIZE 50

class iTupNode
{
public:
    unsigned int r;
    unsigned int c;
    int d;
    iTupNode(unsigned int a,unsigned int b,int c);
    iTupNode();
    ~iTupNode();
};

iTupNode::iTupNode(unsigned int a,unsigned int b,int c)
{
    /**
     * @author: Starfish_DL
     * @description: 前两个参数分别对应所在行与列的数目，最后一个对应的是值
     * @param {int a,int b,int c}
     * @return {*}
     */
    this->r=a;
    this->c=b;
    this->d=c;
}

iTupNode::iTupNode()
{
    this->r=0;
    this->d=0;
    this->c=0;
}

iTupNode::~iTupNode()
{
}

class iTSMatrix
{
private:
    int rows;
    int cols;
    int nums;
    iTupNode data[MAXSIZE];
public:
    iTSMatrix(int dArray[][8],int A,int B);
    iTSMatrix(unsigned int r,unsigned int c,unsigned int n);
    void setValue(unsigned int i,unsigned int j,int s);
    int getValue(unsigned int i,unsigned int j);
    void dispMatrix();
    iTSMatrix tranTat();
    ~iTSMatrix();
};

iTSMatrix::iTSMatrix(int dArray[][8],int A,int B)
{
    this->rows=A;
    this->cols=B;
    this->nums=0;
    for (int a = 0; a < A; a++)
    {
        for (int b = 0; b < B; b++)
        {
            if (this->nums<=MAXSIZE && dArray[a][b]!=0)
            {
                this->data[nums]=iTupNode(a,b,dArray[a][b]);
                this->nums++;
            }
        }
    }
}

iTSMatrix::iTSMatrix(unsigned int r,unsigned int c,unsigned int n)
{
    this->cols=c;
    this->rows=r;
    if (n<=MAXSIZE)
    {
        this->nums=n;
    }
    else
    {
        this->nums=MAXSIZE;
    }
}

iTSMatrix::~iTSMatrix()
{
}

iTSMatrix iTSMatrix::tranTat()
{
    int temp;
    iTSMatrix tran(this->cols,this->rows,0);
    for (int i = 0; i < this->cols; i++)
    {
        for (int k = 0; k < this->nums; k++)
        {
            if (this->data[k].c==i)
            {
                tran.data[tran.nums]=this->data[k];
                temp=tran.data[tran.nums].c;
                tran.data[tran.nums].c=tran.data[tran.nums].r;
                tran.data[tran.nums].r=temp;
                tran.nums++;
            }
        }
    }
    return tran;
}

void iTSMatrix::dispMatrix()
{
    for (int i = 0; i < this->nums; i++)
    {
        cout<<"("<<this->data[i].r<<","<<this->data[i].c<<","<<this->data[i].d<<")"<<endl;
    }
    cout<<"rows:"<<this->rows<<endl;
    cout<<"cols:"<<this->cols<<endl;
    cout<<"nums:"<<this->nums<<endl;
}

void iTSMatrix::setValue(unsigned int i,unsigned int j,int s)
{
    if (i>this->rows || j>this->cols)
    {
        return;
    }
    int k=0;
    while (this->data[k].r<i)
    {
        k++;
    }
    while (k < this->nums)
    {
        if (i==this->data[k].r && j==this->data[k].c)
        {
            this->data[k].d=s;
            return;
        }
        if (this->data[k].c>j)
        {
            break;
        }        
        else if (this->data[k].r>i)
        {
            break;
        }
        k++;
    }
    int t;
    if (this->nums==MAXSIZE)
    {
        t=MAXSIZE-1;
        this->nums=MAXSIZE-1;
    }
    else
    {
        t=this->nums;
    }
    while (t >= k)
    {
        this->data[t+1]=this->data[t];
        t--;
    }
    this->data[k]=iTupNode(i,j,s);
    this->nums++;
}

int iTSMatrix::getValue(unsigned int i,unsigned int j)
{
    if (i>this->rows || j>this->cols)
    {
        return 0;
    }
    int k=0;
    while (this->data[k].r<i)
    {
        k++;
    }
    while (k < this->nums)
    {
        if (i==this->data[k].r && j==this->data[k].c)
        {
            return this->data[k].d;
        }
        if (this->data[k].c>j)
        {
            break;
        }        
        else if (this->data[k].r>i)
        {
            break;
        }
        k++;
    }
    return 0;
}

int main()
{
    int testArray[8][8]={
        {0,1,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0},
        {0,1,0,0,0,0,1,0},
        {0,1,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0},
        {0,1,0,0,0,0,1,0},
        {0,1,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0}
    };
    iTSMatrix testTS=iTSMatrix(testArray,8,8);
    testTS.dispMatrix();
    iTSMatrix testTS2=testTS.tranTat();
    testTS2.setValue(1,0,3);
    testTS2.dispMatrix();
    cout<<testTS2.getValue(1,0);
    return 0;
}